<?php
session_start();
if (empty($_SESSION['username'])) {
    echo '<script type="text/javascript">window.location.href="../index.php";</script>';
} else {
    $now = time();
    if ($now > $_SESSION['expire']) {
        session_destroy();
        echo '<script type="text/javascript">var r=confirm("Session expire (30 mins)!"); if(r==true){window.location.href="../index.php";}else{window.location.href="index.php";}</script>';
    } else {
        require '../model-db-connection/config.php';
        $config = require '../model-db-connection/qrc_conf.properties.php';
    }
}
?>
<script src="../js/jquery.dataTables.js"></script>
<script src="../js/plugins/datatables/ColReorderWithResize.js"></script>
<script src="../js/plugins/datatables/colvis/dataTables.ColVis.min.js"></script>
<script src="../js/plugins/datatables/tabletools/ZeroClipboard.js"></script>
<script src="../js/plugins/datatables/tabletools/dataTables.TableTools.min.js"></script>
<script src="../js/dataTables.bootstrap.js"></script>

<link rel="stylesheet" href="../css/plugins/dataTables.bootstrap.css">
<link rel="stylesheet" href="../css/ColVis.css">

<table class="table table-striped table-bordered table-hover" id="dataTables-member" width="100%">

    <!-- Table heading -->
    <thead>
        <tr>
            <th data-class="expand" class="center">WO Code</th>
            <th data-class="expand" class="center">Project Code</th>
            <th data-hide="phone,tablet">Document No.</th>
            <th data-hide="phone,tablet">PO No.</th>
<!--            <th data-hide="phone,tablet">PO Owner</th>
            <th data-hide="phone,tablet">PO Sender</th>
            <th data-hide="phone,tablet">Date</th>
            <th data-hide="phone,tablet">Order Type</th>
            <th data-hide="phone,tablet">Remark</th>
            <th data-hide="phone,tablet">PO Image</th>                    -->
            <th class="center">WO Status</th>
            <th class="center">Plan</th>
            <th class="center">Plot</th>
            <th class="center">Plan Size</th>
<!--            <th class="center">Unit Price</th>
            <th class="center">Amount</th>
            <th class="center">Included VAT 7%</th>-->
            <th class="center"></th>
        </tr>
    </thead>
    <!-- // Table heading END -->
    <!-- Table body -->
    <tbody>
        <?php
        $sqlSelectAllProjectRecord = "SELECT qpo.project_code as project_code,
                        qpo.project_order_id as order_id,
            qpo.project_order_plan as project_plan,
            qpo.project_order_plot as project_plot,
            qpo.document_no as document_no,
            qpo.po_no as po_no,
            qpo.po_owner as po_owner,
            qpo.po_sender as po_sender,
            qpo.created_date_time as created_date_time,
            qpot.service_name as order_type,
            qpo.plan_size as plan_size,
            qpo.unit_price as unit_price,
            qpo.amount as amount,
            qpo.include_vat as vat,
            qpo.image_name as imgName,
                        qpo.project_status as project_status,
                        qpo.remark as project_order_remark
                        FROM QRC_PROJECT_ORDER qpo
                        LEFT JOIN QRC_TYPE_OF_SERVICE qpot ON qpo.order_type = qpot.service_id
                        ORDER BY qpo.created_date_time DESC;";

        $sqlGetAllData = mysql_query($sqlSelectAllProjectRecord);
        if (mysql_num_rows($sqlGetAllData) >= 1) {
            while ($row = mysql_fetch_assoc($sqlGetAllData)) {

                echo '<tr class = "gradeX">';
                echo '<td>' . $row['order_id'] . '</td>';
                echo '<td>' . $row['project_code'] . '</td>';
                echo '<td>' . $row['document_no'] . '</td>';
                echo '<td>' . $row['po_no'] . '</td>';
//                echo '<td>' . $row['po_owner'] . '</td>';
//                echo '<td>' . $row['po_sender'] . '</td>';
//                echo '<td>' . $row['created_date_time'] . '</td>';
//                echo '<td>' . $row['order_type'] . '</td>';
//                echo '<td>' . $row['project_order_remark'] . '</td>';

                $selectImageFromQRCPOIMG = "SELECT IMAGE_PATH AS IMAGE_PATH FROM QRC_PO_IMAGE WHERE TEMP_PO_ID LIKE '" . $row['imgName'] . "'";
                $sqlGetAllDatas = mysql_query($selectImageFromQRCPOIMG);
                while ($rop = mysql_fetch_assoc($sqlGetAllDatas)) {
                    //echo '<td><d class="fancybox-effects-d" href="images/uploads/' . $rop['IMAGE_PATH'] . '" title="' . $rop['IMAGE_PATH'] . '" onlick="changeAttrHref()"><img src="images/uploads/' . $rop['IMAGE_PATH'] . '" alt="Smiley face" width="200"></d></td>';
                }
                if ($row['project_status'] == "New") {
                    echo '<td class = "center" style="color: blue;">' . $row['project_status'] . '</td>';
                } else if ($row['project_status'] == "Complete") {
                    echo '<td class = "center" style="color: green;">' . $row['project_status'] . '</td>';
                } else if ($row['project_status'] == "Assign") {
                    echo '<td class = "center" style="color: orange;">' . $row['project_status'] . '</td>';
                } else if ($row['project_status'] == "Cancel") {
                    echo '<td class = "center" style="color: red;">' . $row['project_status'] . '</td>';
                } else {
                    echo '<td class = "center">' . $row['project_status'] . '</td>';
                }
                echo '<td class = "center">' . $row['project_plan'] . '</td>';
                echo '<td class = "center">' . $row['project_plot'] . '</td>';
                echo '<td class = "center">' . $row['plan_size'] . '</td>';
//                echo '<td class = "center">' . $row['unit_price'] . '</td>';
//                echo '<td class = "center">' . $row['amount'] . '</td>';
//                echo '<td class = "center">' . $row['vat'] . '</td>';



                echo '<td>';

                echo '<div class = "btn-group margin-bottom-20">';
                echo '<button type = "button" class = "btn btn-default dropdown-toggle btn-xs" data-toggle = "dropdown">Actions <span class = "caret"></span></button>';

                echo '<ul class = "dropdown-menu" role = "menu">';
                echo '<li><a href = "#modal-project-order" class="btn-xs" data-toggle = "modal" onclick=editPO("' . $row['project_code'] . '","' . $row['order_id'] . '")><i class = "fa fa-edit"></i> Edit (แก้ไข)</a></li>';
                echo '<li><a href = "#" class="btn-xs" onclick=viewClick("' . $row['order_id'] . '")><i class = "fa fa-eye"></i> View (ดูข้อมูล)</a></li>';
                echo '<li class = "divider"></li>';
                echo '<li><a href = "#" class="btn-xs" onclick=deletePO("' . $row['order_id'] . '","' . $row['project_code'] . '","' . $row['imgName'] . '")><i class = "fa fa-trash-o"></i> Delete (ลบ)</a></li>';
                echo '</ul>';
                echo '</div>';
                echo '</td>';
                echo '</tr>';
            }
        }
        ?>
    </tbody>
    <!-- // Table body END -->
</table>
<script type="text/javascript" src="../js/zebra_dialog.js"></script>
<link rel="stylesheet" href="../css/zebra_dialog.css" type="text/css">
<script>
    $(document).ready(function() {
        var datableConfig = $('#dataTables-member').dataTable({
            "order": [[0, "desc"]]
        });
    });
    function viewClick(po_id) {
        $.Zebra_Dialog({
            source: {'ajax': 'AjaxViewContent.php?po_id=' + po_id},
            width: 600,
            title: 'Work Order Detail'
        });
    }
</script>